package com.yvon.service.demo.spring.factory.support;

import com.yvon.service.demo.spring.BeanException;
import com.yvon.service.demo.spring.factory.config.BeanDefinition;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认bean工厂
 *
 * 最终的bean工厂，缓存了BeanDefinition的Map，实现获取BeanDefinition的方法：从BeanDefinitionMap里获取
 *
 * @author Yvon
 * @since 2022-11-24
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry{

    private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();

    @Override
    protected BeanDefinition getBeanDefinition(String beanName) throws BeanException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new BeanException("No bean named" + beanName + ", is defined");
        }
        return beanDefinition;

    }

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }
}
